package com.xtravel.common.widget;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.GridView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by linjiale on 2015-10-14.
 */
public class BottomMenuGridView extends GridView {
    private boolean isAutoNumColumns;
    private int realHeight = 0;
    private int readNumColumns = -1;
    private int resizeMeasureHeight = MeasureSpec.UNSPECIFIED;
    private boolean isResized = false;

    public BottomMenuGridView(Context context) {
        super(context);
    }

    public BottomMenuGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public BottomMenuGridView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        isAutoNumColumns = getNumColumns() == -1;
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (isResized) {
            heightMeasureSpec = resizeMeasureHeight;
        } else {
            int childCount = getChildCount();
            Log.d("BottomMenuGridView", "childCount:" + childCount);
            if (childCount > 0) {
                int measureWidth = MeasureSpec.getSize(widthMeasureSpec);
                //获取gridview可操作宽度
                int gridWidth = measureWidth;
                gridWidth -= getPaddingLeft();
                gridWidth -= getPaddingRight();

                int numColumns = 0;
                if (isAutoNumColumns) {
                    //列数自适应
                    List<Integer> childrenWidth = new ArrayList<>();
                    int Horizontalspace = getHorizontalSpacing();
                    for (int i = 0; i < childCount; i++) {
                        childrenWidth.add(getChildAt(i).getMeasuredWidth());
                    }

                    //按照每个菜单项的宽度从大至小排序
                    Collections.sort(childrenWidth, new Comparator<Integer>() {
                        @Override
                        public int compare(Integer arg0, Integer arg1) {
                            if (arg0 > arg1) {
                                return -1;
                            } else if (arg0 == arg1) {
                                return 0;
                            }
                            return 1;
                        }
                    });
                    int mulChildWidth = 0;

                    //计算最多可显示的列数
                    for (int i = 0; i < childrenWidth.size(); i++) {
                        if (mulChildWidth > 0) {
                            mulChildWidth += Horizontalspace;
                        }
                        mulChildWidth += childrenWidth.get(i);
                        if (gridWidth < mulChildWidth) {
                            numColumns = i;
                            break;
                        } else if (gridWidth == mulChildWidth || i + 1 == childrenWidth.size()) {
                            numColumns = i + 1;
                            break;
                        }
                    }
                    readNumColumns = numColumns;
                    //高度自适应
                    int totalHeight = 0;
                    int rowMaxHeight = 0;
                    int childHeight = 0;
                    int VerticalSpacing = getVerticalSpacing();
                    for (int i = 0; i < childCount; i += numColumns) {
                        for (int j = 0; j < Math.min(childCount - i, numColumns); j++) {
                            childHeight = getChildAt(i + j).getMeasuredHeight();
                            rowMaxHeight = rowMaxHeight < childHeight ? childHeight : rowMaxHeight;
                        }
                        if (totalHeight > 0) {
                            totalHeight += VerticalSpacing;
                        }
                        totalHeight += rowMaxHeight;
                    }
                    realHeight = totalHeight;
                    resizeMeasureHeight = heightMeasureSpec = MeasureSpec.makeMeasureSpec(realHeight, MeasureSpec.EXACTLY);
                    setNumColumns(readNumColumns);
                    isResized = true;
                }
            }
        }
        setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
    }
}
